package com.second.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.second.consts.RabbitConst;

import java.util.HashMap;
import java.util.Map;

/**
 * 本类用来演示: 死信队列
 *
 * @author: success
 * @date: 2022/4/26 10:28 上午
 */
@Configuration
public class RabbitDeadConfig {
    @Bean
    public DirectExchange orderDirectExchange() {
        return new DirectExchange(RabbitConst.ORD_DIRECT_EX);
    }

    @Bean
    public Queue orderQueue() {
        //需要绑定到死信队列的交换机以及路由
        Map<String, Object> args = new HashMap<>();

        args.put("x-dead-letter-exchange", RabbitConst.ORD_DEAD_DIRECT_EX);
        args.put("x-dead-letter-routing-key", RabbitConst.ORD_DEAD_QUEUE_RK);

        return new Queue(RabbitConst.ORD_QUEUE, true, false, false, args);
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderDirectExchange()).with(RabbitConst.ORD_QUEUE_RK);
    }

    @Bean
    public DirectExchange ordDeadDirectExchange() {
        return new DirectExchange(RabbitConst.ORD_DEAD_DIRECT_EX);
    }

    @Bean
    public Queue ordDeadQueue() {
        return new Queue(RabbitConst.ORD_DEAD_QUEUE);
    }

    @Bean
    public Binding ordDeadBinding() {
        return BindingBuilder.bind(ordDeadQueue()).to(ordDeadDirectExchange()).with(RabbitConst.ORD_DEAD_QUEUE_RK);
    }

}
